//
//  ChooseCompetitorController.m
//  YunGui
//
//  Created by Hanen 3G 01 on 16/4/15.
//  Copyright © 2016年 hanen. All rights reserved.
//

#import "ChooseCompetitorController.h"
#import "ChooseCompetitorCell.h"
#import "SettingButton.h"

@interface ChooseCompetitorController ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView  *_tablview;
    
    NSMutableArray  *_dataArr;
    
    NSMutableArray  *_chooseArr;
}
@end

@implementation ChooseCompetitorController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"竞争对手";
    _dataArr = [NSMutableArray array];
    
    _selectedArray = [NSMutableArray arrayWithArray:_foreArr];
    
    _chooseArr = [NSMutableArray arrayWithArray:_foreDictArr];
    
    self.navigationItem.leftBarButtonItem = [ViewTool getBarButtonItemWithTarget:self WithString:@"back" WithAction:@selector(goToBack)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(getBackCompetitor:)];
    _tablview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight  -64) style:UITableViewStylePlain];
    _tablview.delegate = self;
    _tablview.dataSource = self;
    [self.view addSubview:_tablview];
    [self getCompatitorData];
//    _tablview.editing = YES;
    _tablview.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)getCompatitorData
{
    int isCache;
    NSString *currentTime = [DateTool getCurrentDate];
    NSString *timeString = [[NSUserDefaults standardUserDefaults] objectForKey:@"getCompatitorTime"];
    NSLog(@"%@%@",currentTime,timeString);
    if (timeString != nil) {
        NSInteger diffTime = [DateTool getTimesDiffTimeFrom:timeString To:currentTime];
        if (diffTime > 24) {
            isCache = 1;
        }else{
            isCache = 0;
        }
    }
    
    [[NSUserDefaults standardUserDefaults] setObject:currentTime forKey:@"getCompatitorTime"];
    
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:@{@"method" : @"app.village.competitor",@"isCache" :@(isCache)}];
    NSDictionary *parames = [DataTool addSignToDic:dict];
    [DataTool postWithUrl:HEAD_URL parameters:parames success:^(id data) {
        id backData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments | NSJSONReadingMutableContainers error:nil];
                NSLog(@"竞争对手的数据 :%@",backData);
        _dataArr = backData[@"data"][@"allCompetitor"];
        [_tablview reloadData];
    } fail:^(NSError *error) {
        NSLog(@"%@",error.localizedDescription);
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChooseCompetitorCell *cell = [  ChooseCompetitorCell cellWithTabelView:tableView];
//    [cell.settingBtn setTitle:_dataArr[indexPath.row][@"name"] forState:UIControlStateNormal];
    cell.dataDict = _dataArr[indexPath.row];
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.row = indexPath.row;
    __unsafe_unretained ChooseCompetitorController * vc = self;
    cell.selectedBlcok = ^(BOOL selected, NSInteger row){
//          NSLog(@"选中的数组%ld",[vc.selectedArray count]);
        if (selected) {
            [vc.selectedArray addObject:_dataArr[indexPath.row][@"name"] ];
            [_chooseArr addObject:_dataArr[indexPath.row]];
        }else{
            [vc.selectedArray removeObject:_dataArr[indexPath.row][@"name"] ];
            [_chooseArr removeObject:_dataArr[indexPath.row]];
        }
//       NSLog(@"删除选中的数组后%ld",[vc.selectedArray count]);
    };
    return cell;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete & UITableViewCellEditingStyleInsert;
    
    
    //UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert; 显示系统自带的多选
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(ChooseCompetitorCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
//    NSString *name = cell.settingBtn.currentTitle;
    
    
    if ([self.selectedArray containsObject:cell.settingBtn.currentTitle]) {
        cell.settingBtn.isSelected = YES;
//       [cell.settingBtn setImage:[ViewTool getImageFromColor:BlueColor WithRect:CGRectMake(0, 0, 15, 15)] forState:UIControlStateNormal];
        [cell.settingBtn setImage:[UIImage imageNamed:@"Btn_Normal_dui"] forState:UIControlStateNormal];
    }else
    {
           cell.settingBtn.isSelected = NO;
//         [cell.settingBtn setImage:[ViewTool getImageFromColor:[UIColor whiteColor] WithRect:CGRectMake(0, 0, 15, 15)] forState:UIControlStateNormal];
        [cell.settingBtn setImage:[UIImage imageNamed:@"noselected1"] forState:UIControlStateNormal];
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
//    NSDictionary *dict = _dataArr[indexPath.row];
//    NSString *ids = dict[@"name"];
//    if ([_selectedArray containsObject:ids]) {
//        [_selectedArray removeObject:ids];
//    }else {
//        [_selectedArray addObject:ids];
//    }
//    NSIndexPath * inpath = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
//    [_tablview reloadRowsAtIndexPaths:[NSArray arrayWithObject:inpath] withRowAnimation:UITableViewRowAnimationNone];
//    [_tablview reloadData];
}

- (void)getBackCompetitor:(UIButton *)btn
{
    if (self.blcok) {
        NSLog(@"_chooseArray.count -- %ld",(unsigned long)_chooseArr.count);
        self.blcok(_chooseArr);

    }
    [self.navigationController popViewControllerAnimated:NO];
}
- (void)goToBack{
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
